package com.android.plc.ui.manager;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.plc.R;
import com.android.plc.classholder.BasicItem;
import com.android.plc.classholder.Device;
import com.android.plc.classholder.Profile;
import com.android.plc.classholder.Room;
import com.android.plc.ui.ControlManager;
import com.android.plc.ui.IBaseManager;
import com.android.plc.ui.IManager;
import com.android.plc.ui.view.BaseControlView;

public abstract class BaseManager<T extends BasicItem> implements IManager<T>, OnClickListener, IBaseManager,
		TextWatcher
{
	public Context mContext;
	public ViewGroup mCtrolView;
	private ControlManager mControlManager;
	public static String TAG = "BaseManager";

	public BaseManager(Context context, ViewGroup view, int res_id)
	{
		mContext = context;
		mCtrolView = new BaseControlView(mContext, res_id);
	};

	public abstract void initView(ViewGroup view);

	public abstract void displayItem(T item);

	public abstract boolean checkValidity(View view);

	public abstract Uri getUri();

	public abstract ContentValues item2maps(T newT);

	public ViewGroup getView()
	{
		return mCtrolView;
	}

	@SuppressWarnings("finally")
	public boolean insert(T newT)
	{
		ContentResolver resolver = mContext.getContentResolver();
		ContentValues values = item2maps(newT);
		int res_id = 0;
		try
		{
			Uri insertRes = resolver.insert(getUri(), values);
			String _id = insertRes.getPathSegments().get(1);
			res_id = Integer.valueOf(_id);
			Toast.makeText(mContext, R.string.tips_insert_succ, Toast.LENGTH_LONG).show();
			newT._id = res_id;
			mControlManager.onItemSelected(newT);
		} catch (Exception e)
		{
		}
		return res_id > 0;
	}

	@SuppressWarnings("finally")
	public boolean update(T newT)
	{
		ContentResolver resolver = mContext.getContentResolver();
		ContentValues values = item2maps(newT);
		int res = 0;
		try
		{
			String where = "_id = " + newT._id;
			res = resolver.update(getUri(), values, where, null);
		} catch (Exception e)
		{
		} finally
		{
			return res > 0;
		}
	}

	public boolean delete(final T newT)
	{
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setMessage(R.string.tips_delete);
		builder.setTitle(R.string.tips);
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
				ContentResolver resolver = mContext.getContentResolver();
				int res = 0;
				try
				{
					String where = "_id = " + newT._id;
					res = resolver.delete(getUri(), where, null);
					if (res > 0)
					{
						Toast.makeText(mContext, R.string.tips_delete_succ, Toast.LENGTH_LONG).show();
					}
				} catch (Exception e)
				{
				}
			}
		});
		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		});
		builder.create().show();
		// return res > 0;
		return true;
	}

	public void onBasicItemSelected(Manager mgr, BasicItem item, boolean admin)
	{
	}

	public void setControlManager(ControlManager mControlManager)
	{
		this.mControlManager = mControlManager;
	}

	public ControlManager getControlManager()
	{
		return mControlManager;
	}

	public Device getDevice()
	{
		return mControlManager.getDevice();
	}

	public Room getRoom()
	{
		return mControlManager.getRoom();
	}

	public Profile getProfile()
	{
		return mControlManager.getProfile();
	}

	public void attachToControlManager(ViewGroup view)
	{
		view.addView(mCtrolView);
		initView(view);
	}

	public void detachFromControlManager(ViewGroup view)
	{
		view.removeView(mCtrolView);
	}

	public void afterTextChanged(Editable s)
	{
		Log.d(TAG, "afterTextChanged");
	}

	public void beforeTextChanged(CharSequence s, int start, int count, int after)
	{
		Log.d(TAG, "beforeTextChanged");
	}

	public void onTextChanged(CharSequence s, int start, int before, int count)
	{
		Log.d(TAG, "onTextChanged");
	}

}
